import {
  customerrevokeslideorder,
  provideracceptrevokeslideorder,
  providerrejectrevokeslideorder,
  customerrevokeslidehedgeorder
} from "@/api/order";

export const btnRevoke = [
  {
    name: "申请撤单",
    clickEvent: "revokeOrderHedge",
    flag: "revokeOrderHedge"
  },
  {
    name: "申请撤单",
    clickEvent: "revokeOrder",
    flag: "revokeOrder"
  },
  {
    name: "同意撤单",
    clickEvent: "acceptRevokeOrder",
    flag: "acceptRevokeOrder"
  },
  {
    name: "拒绝撤单",
    clickEvent: "rejectRevokeOrder",
    flag: "rejectRevokeOrder"
  }
];

export const revoke = {
  methods: {
    // 买家，申请撤单（套保）
    revokeOrderHedge(row) {
      let _this = this;
      this.$confirm({
        title: "此操作将申请撤销订单, 是否继续?",
        onOk() {
          return new Promise((resolve, reject) => {
            customerrevokeslidehedgeorder({
              orderId: row.id,
              version: row.version
            })
              .then(res => {
                _this.$message.success("操作成功");
                _this.$emit("handleSuccess", {
                  data: res,
                  type: "revokeOrderHedge"
                });
                resolve();
              })
              .catch(() => {
                reject();
              });
          });
        }
      });
    },
    // 买家，申请撤单
    revokeOrder(row) {
      let _this = this;
      this.$confirm({
        title: "此操作将申请撤销订单, 是否继续?",
        onOk() {
          return new Promise((resolve, reject) => {
            customerrevokeslideorder({
              orderId: row.id,
              version: row.version
            })
              .then(res => {
                _this.$message.success("操作成功");
                _this.$emit("handleSuccess", {
                  data: res,
                  type: "revokeOrder"
                });
                resolve();
              })
              .catch(() => {
                reject();
              });
          });
        }
      });
    },
    // 卖家，同意撤单
    acceptRevokeOrder(row) {
      let _this = this;
      this.$confirm({
        title: "此操作将取消该订单, 是否继续?",
        onOk() {
          return new Promise((resolve, reject) => {
            provideracceptrevokeslideorder({
              orderId: row.id,
              version: row.version
            })
              .then(res => {
                _this.$message.success("操作成功");
                _this.$emit("handleSuccess", {
                  data: res,
                  type: "acceptRevokeOrder"
                });
                resolve();
              })
              .catch(() => {
                reject();
              });
          });
        }
      });
    },
    // 卖家，拒绝撤单
    rejectRevokeOrder(row) {
      providerrejectrevokeslideorder({
        orderId: row.id,
        version: row.version
      }).then(res => {
        this.$message.success("操作成功");
        this.$emit("handleSuccess", { data: res, type: "rejectRevokeOrder" });
      });
    }
  }
};
